#!/bin/env python
# -*- coding: utf-8 -*-

import sys
from StringIO import StringIO
from PyQt4 import QtCore,QtGui,uic
from lxml import etree

import exchange_propertys


#是否含有菜单
has_menu_bar    = False
#是否含有状态栏
has_status_bar  = False


#QPushButton 对应 <input type='button'/>
def get_tag_QPushButton(qt_ele) :
  html_tag = etree.Element('input')
  html_tag.set('type','button')
  #html_tag.set('value',''.join(qt_ele.xpath('@name')))
  #添加属性，如绝对位置等
  str_style = exchange_propertys.styles_to_str(exchange_propertys.get_ele_styles(qt_ele,False,'100%',''))
  #设置按钮上显示内容
  html_tag.set('value',''.join(qt_ele.xpath('property[@name="text"]/string/text()')))

  if str_style != '' :  html_tag.set('style',str_style )
  return html_tag

#QLineEdit 对应 <input type='text'/>
def get_tag_QLineEdit(qt_ele) :
  html_tag = etree.Element('input')
  html_tag.set('type','text')
  html_tag.set('value',''.join(qt_ele.xpath('property[@name="text"]/string/text()')))
  #添加属性，如绝对位置等
  str_style = exchange_propertys.styles_to_str(exchange_propertys.get_ele_styles(qt_ele,False,'100%',''))
  #设置文件框上显示内容
  html_tag.text = ''.join(qt_ele.xpath('property[@name="plainText"]/string/text()'))

  if str_style != '' : html_tag.set('style',str_style )
  return html_tag

#QCheckBox 对应 <input type='checkbox'/>
def get_tag_QCheckBox(qt_ele) :
  html_div = etree.Element('span')
  html_tag = etree.Element('input')
  html_tag.set('type','checkbox')
  #添加属性，如绝对位置等
  str_style = exchange_propertys.styles_to_str(exchange_propertys.get_ele_styles(qt_ele,False,'100%',''))
  #设置QCheckBox旁边显示内容
  html_div.text = ''.join(qt_ele.xpath('property[@name="text"]/string/text()'))
  html_div.append(html_tag)

  if str_style != '' : html_div.set('style',str_style )
  return html_div

#QComboBox 对应 <select/>
def get_tag_QComboBox(qt_ele) :
  html_select = etree.Element('select')
  #添加属性，如绝对位置等
  str_style = exchange_propertys.styles_to_str(exchange_propertys.get_ele_styles(qt_ele,False,'100%',''))
  #添加选项
  for qt_option in qt_ele.xpath('item') :
    html_option = etree.Element('option')
    html_option.text = ''.join(qt_option.xpath('property/string/text()'))
    html_select.append(html_option)
    
  if str_style != '' : html_select.set('style',str_style )
  return html_select

#QLabel 对应 <div/>
def get_tag_QLabel(qt_ele) :
  html_tag = etree.Element('div')
  str_text = ''.join(qt_ele.xpath('property[@name="text"]/string/text()'))
  #添加属性，如绝对位置等
  str_style = exchange_propertys.styles_to_str(exchange_propertys.get_ele_styles(qt_ele,False,'100%',''))
  #img:开头的是图片
  if str_text[0:4] == 'img:' :
    html_tag.tag = 'img'
    html_tag.set('src',str_text[4:])
  #其它是文字
  else :
    html_tag.text = str_text
  if str_style != '' : html_tag.set('style',str_style )
  return html_tag

#QTextEdit 对应 <div/>
def get_tag_QTextEdit(qt_ele) :
  html_tag = etree.Element('div')
  #添加属性，如绝对位置等
  str_style = exchange_propertys.styles_to_str(exchange_propertys.get_ele_styles(qt_ele,True,'100%','100%'))
  #添加 html 格式的内容
  html_text = ''.join(qt_ele.xpath('property[@name="html"]/string/text()'))
  #如果有内容
  if html_text != '' :
    #只取body中内容加入控件
    html_text_tag = etree.parse(StringIO(html_text)).getroot()
    html_text_tag_body = html_text_tag.xpath('/html/body')
    if html_text_tag_body is not None :
      html_text_tag_body[0].tag = 'div'
      html_tag.append(html_text_tag_body[0])
      #添加断开单词属性
      str_style += 'word-break:break-all;word-wrap:break-word;'
      #添加滚动条style
      str_style += 'overflow:auto;'

  if str_style != '' : html_tag.set('style',str_style )
  return html_tag

#QPlainTextEdit 对应 <div/>
def get_tag_QPlainTextEdit(qt_ele) :
  html_tag = etree.Element('textarea')
  #添加属性，如绝对位置等
  str_style = exchange_propertys.styles_to_str(exchange_propertys.get_ele_styles(qt_ele,True,'100%','100%'))
  #取内容，替换回车
  str_content = ''.join(qt_ele.xpath('property[@name="plainText"]/string/text()'))
  #如果有内容
  if str_content != '' :
    html_tag.text = str_content
    #添加断开单词属性
    str_style += 'word-break:break-all;word-wrap:break-word;resize:none;'
    #添加滚动条style
    str_style += 'overflow:auto;'
    
  if str_style != '' : html_tag.set('style',str_style )
  return html_tag

#QTextBrowser 对应 <div/>
def get_tag_QTextBrowser(qt_ele) :
  html_tag = etree.Element('div')
  #添加属性，如绝对位置等
  str_style = exchange_propertys.styles_to_str(exchange_propertys.get_ele_styles(qt_ele,True,'100%','100%'))
  #添加 html 格式的内容
  html_text = ''.join(qt_ele.xpath('property[@name="html"]/string/text()'))
  #如果有内容
  if html_text != '' :
    #只取body中内容加入控件
    html_text_tag = etree.parse(StringIO(html_text)).getroot()
    html_text_tag_body = html_text_tag.xpath('/html/body')
    if html_text_tag_body is not None :
      html_text_tag_body[0].tag = 'div'
      html_tag.append(html_text_tag_body[0])
      #添加断开单词属性
      str_style += 'word-break:break-all;word-wrap:break-word;'
      #添加滚动条style
      str_style += 'overflow:auto;'
    
  if str_style != '' : html_tag.set('style',str_style )
  return html_tag

#QTableWidget 对应 <table/>
def get_tag_QTableWidget(qt_ele) :
  html_tag = etree.Element('table')
  html_tag.set('border','1')
  #取属性，如绝对位置等
  str_style = exchange_propertys.styles_to_str(exchange_propertys.get_ele_styles(qt_ele,True,'100%','100%'))
  #添加边框属性
  str_style += 'border=1px;'
  #添加断开单词属性
  str_style += 'word-break:break-all;word-wrap:break-word;resize:none;'
  #添加滚动条style
  str_style += 'overflow:auto;'
  #添加行头
  html_tag_th = etree.Element('tr')
  html_tag_th.set('style','background:#CFCFCF;')
  html_tag_th.append(etree.Element('td'))
  html_tag.append(html_tag_th)
  for col in qt_ele.xpath('column') :
    html_tag_td = etree.Element('td')
    html_tag_td.text = ''.join(col.xpath('property/string/text()'))
    html_tag_th.append(html_tag_td)
  #添加行列
  for row in qt_ele.xpath('row') :
    #添加行单元格
    html_tag_tr = etree.Element('tr')
    html_tag.append(html_tag_tr)
    #添加行头列
    html_tag_tmp = etree.Element('td')
    html_tag_tmp.text = ''.join(row.xpath('property/string/text()'))
    html_tag_tmp.set('style','background:#CFCFCF;')
    html_tag_tr.append(html_tag_tmp)
    for col in qt_ele.xpath('column') :
      html_tag_td = etree.Element('td')
      html_tag_tr.append(html_tag_td)
  #添加数据
  for qt_item in qt_ele.xpath('item') :
    i = int(''.join(qt_item.xpath('@row')))
    j = int(''.join(qt_item.xpath('@column')))
    html_tag[i+1][j+1].text = ''.join(qt_item.xpath('property/string/text()'))

  if str_style != '' : html_tag.set('style',str_style )
  return html_tag

#QTreeWidget 对应 <div/>
def recu_add_item(qt_ele,html_tag) :
  html_tag_ul = etree.Element('ul')
  for qt_item in qt_ele.xpath('item') :
    html_tag_li = etree.Element('li')
    html_tag_li.text = ''.join(qt_item.xpath('property/string/text()'))
    html_tag_ul.append(html_tag_li)
    recu_add_item(qt_item,html_tag_li)
  if len(html_tag_ul) > 0 :
    html_tag.append(html_tag_ul)

def get_tag_QTreeWidget(qt_ele) :
  html_tag = etree.Element('div')
  #取属性，如绝对位置等
  str_style = exchange_propertys.styles_to_str(exchange_propertys.get_ele_styles(qt_ele,True,'100%','100%'))
  #添加边框属性
  str_style += 'border=1px;'
  #添加断开单词属性
  str_style += 'word-break:break-all;word-wrap:break-word;resize:none;'
  #添加滚动条style
  str_style += 'overflow:auto;'
  #添加树
  html_tag_ul = etree.Element('ul')
  for qt_item in qt_ele.xpath('item') :
    html_tag_li = etree.Element('li')
    html_tag_li.text = ''.join(qt_item.xpath('property/string/text()'))
    html_tag_ul.append(html_tag_li)
    recu_add_item(qt_item,html_tag_li)
  if len(html_tag_ul) > 0 : html_tag.append(html_tag_ul)
  if str_style != '' : html_tag.set('style',str_style )
  return html_tag

#QMenuBar 对应 <ul/>
def recu_add_QMenu(qt_ele,html_tag) :
  html_tag_ul = etree.Element('ul')
  html_tag_ul.set('style','display:none;')
  #
  for menu_item in qt_ele.xpath('*') :
    html_tag_li = etree.Element('li')
    html_tag_li.set('style','float:left;width:100px;')
    if menu_item.tag == 'addaction' :
      action_item_name = ''.join(menu_item.xpath('@name'))
      if action_item_name != 'separator' :
        html_tag_li.text = ''.join(qt_ele.xpath('/ui/widget/action[@name="%s"]/property/string/text()'%action_item_name))
      else:
        html_tag_li.text = '---'
      html_tag_ul.append(html_tag_li)
    elif menu_item.tag == 'widget' :
      html_tag_li.text = ''.join(menu_item.xpath('property/string/text()'))
      html_tag_ul.append(html_tag_li)
      recu_add_QMenu(menu_item,html_tag_li)
  if len(html_tag_ul) > 0 :
    html_tag.append(html_tag_ul)

def get_tag_QMenuBar(qt_ele) :
  global has_menu_bar
  has_menu_bar = True
  html_tag = etree.Element('div')
  #取属性，如绝对位置等
  str_style  = 'position:absolute;'
  str_style += exchange_propertys.styles_to_str(exchange_propertys.get_ele_styles(qt_ele,True,'100%','35px','0px','0px'))
  #添加边框属性
  str_style += 'border:3px solid #E1E1E1;'
  #添加菜单项
  html_tag_ul = etree.Element('ul')
  for menu_item in qt_ele.xpath('widget[@class="QMenu"]') :
    html_tag_li = etree.Element('li')
    html_tag_li.set('style','float:left;width:100px;')
    html_tag_li.set('onmouseover','on_menu_item_mouseover(this);')
    html_tag_li.set('onmouseout' ,'on_menu_item_mouseout(this);')
    html_tag_li.text = ''.join(menu_item.xpath('property/string/text()'))
    html_tag_ul.append(html_tag_li)
    recu_add_QMenu(menu_item,html_tag_li)
  if len(html_tag_ul) > 0 : html_tag.append(html_tag_ul)
  if str_style != '' : html_tag.set('style',str_style )
  return html_tag


#QStatusBar 对应 <div/>
def get_tag_QStatusBar(qt_ele) :
  global has_status_bar
  has_status_bar = True
  html_tag = etree.Element('div')
  #取属性，如绝对位置等
  styles = exchange_propertys.get_ele_styles(qt_ele,True,'100%','35px')
  styles['position'] = 'absolute'
  #添加边框属性
  styles['border'] = '3px solid #E1E1E1'
  if not styles.has_key('left') : 
    styles['left'] = '0px'
    styles['bottom'] = '0px'
  str_style = exchange_propertys.styles_to_str(styles)
  html_tag.set('style',str_style )
  return html_tag




#layout 对应 <table/>
def get_tag_layout(qt_ele) :
  html_tag = etree.Element('table')
  html_tag.set('style','height:100%;width:100%;table-layout:fixed;border-collapse:collapse;border:#0FF solid 2px;')
  return html_tag

#widget 可能会对应多种控件
def get_tag_widget(qt_ele) :
  #查看类型
  str_class = ''.join(qt_ele.xpath('@class'))
  if str_class == 'QPushButton' :
    return get_tag_QPushButton(qt_ele)
  elif str_class == 'QLineEdit' :
    return get_tag_QLineEdit(qt_ele)
  elif str_class == 'QCheckBox' :
    return get_tag_QCheckBox(qt_ele)
  elif str_class == 'QTextEdit' :
    return get_tag_QTextEdit(qt_ele)
  elif str_class == 'QPlainTextEdit' :
    return get_tag_QPlainTextEdit(qt_ele)
  elif str_class == 'QComboBox' :
    return get_tag_QComboBox(qt_ele)
  elif str_class == 'QLabel' :
    return get_tag_QLabel(qt_ele)
  elif str_class == 'QTextBrowser' :
    return get_tag_QTextBrowser(qt_ele)
  elif str_class == 'QTableWidget' :
    return get_tag_QTableWidget(qt_ele)
  elif str_class == 'QTreeWidget' :
    return get_tag_QTreeWidget(qt_ele)
  elif str_class == 'QCommandLinkButton' :
    return get_tag_QPushButton(qt_ele)
  elif str_class == 'QMenuBar' :
    return get_tag_QMenuBar(qt_ele)
  elif str_class == 'QStatusBar' :
    return get_tag_QStatusBar(qt_ele)
    
  #不认识的类型
  else :
    print 'get_tag_widget error : unknown tag : ' + qt_ele.tag + ''.join(qt_ele.xpath('@class'))
    html_tag = etree.Element('unknown')
    return html_tag
